﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    static int Attempt = 5;
    static int i = 0;
    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection(@"Data Source=PRIYANUDEEP\PRIYANUDEEP;Initial Catalog=ASPM_Project;Integrated Security=True");
        
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("select word from Tbl_WordPuzzle", con);
        da.Fill(ds, "Puzzle");
             
        if (!IsPostBack)
            LoadWord(ds.Tables[0].Rows[i][0].ToString());
        //Response.Write(ds.Tables[0].Rows[i][0].ToString());
    }
    public void LoadWord(string Input)
    {
        Label5.Text = Input[0].ToString();
        Label6.Text = Input[1].ToString();
        Label7.Text = Input[2].ToString();
        Label8.Text = Input[3].ToString();
        Label9.Text = Input[4].ToString();
        TextBox2.Text=Input[1].ToString();
        TextBox4.Text = Input[3].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Attempt <= 0)
            Response.Redirect("HomePage.aspx");
        bool ans;
        if (TextBox1.Text != "" && TextBox1.ReadOnly==false)
        {
            ans = check(TextBox1, Label5);
            if(!ans)
            {
                string script = "alert('Incorrect!')";
                ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
                Attempt = Attempt - 1;
                Label3.Text = Attempt.ToString();
            }
        }
        if (TextBox3.Text != "" && TextBox3.ReadOnly==false)
        {
            ans = check(TextBox3, Label7);
            if (!ans)
            {
                string script = "alert('Incorrect!')";
                ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
                Attempt = Attempt - 1;
                Label3.Text = Attempt.ToString();
            }
        }
        if (TextBox5.Text != "" && TextBox5.ReadOnly==false)
        {
            ans = check(TextBox5, Label9);
            if (!ans)
            {
                string script = "alert('Incorrect!')";
                ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
                Attempt = Attempt - 1;
                Label3.Text = Attempt.ToString();
            }
        }
    }
    public bool check(TextBox tbx, Label lbl)
    {
        if (tbx.Text != ""&&tbx.ReadOnly==false)
        {
            if (tbx.Text == lbl.Text)
            {
                string script = "alert('Correct!')";
                ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
                tbx.ReadOnly = true;
                return true;
            }
        }
        return false;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        i = i + 1;
        TextBox1.Text = TextBox3.Text = TextBox5.Text = "";
        TextBox1.ReadOnly = TextBox3.ReadOnly = TextBox5.ReadOnly = false;
        SqlDataAdapter da = new SqlDataAdapter("select word from Tbl_WordPuzzle", con);
        da.Fill(ds, "Puzzle");
        LoadWord(ds.Tables[0].Rows[i][0].ToString());
    }
}